package com.sky.controller.admin;

import com.aliyun.oss.AliyunOssOperator;
import com.sky.constant.RubbishSetConstant;
import com.sky.exception.BaseException;
import com.sky.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
public class CommonController {
    @Autowired
    private AliyunOssOperator aliyunOssOperator;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){

        try {
            // 获取原始文件名
            String originalFilename = file.getOriginalFilename();
            // 生成新的文件名:新的字符串
            String realName = UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));

            //调用阿里云的上传接口
       String url = aliyunOssOperator.upload(realName, file.getInputStream());
       //TODO:存入set集合中，方便后续删除
            RubbishSetConstant.RUBBISH_SET.add(url);
       return Result.success(url);
        }catch (Exception e){
            throw new BaseException("图片上传出错");
        }

    }
}
